package com.Bigbluebook.Service;


import com.Bigbluebook.api.ApiConstants;
import com.Bigbluebook.entity.Message;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MessageApiService {
    private MessageService messageService;
    private Retrofit retrofit;
    private Gson gson;

    public MessageApiService() {
        gson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
                .create();
        retrofit = new Retrofit.Builder()
                .baseUrl(ApiConstants.BASE_URL) // 确保ApiConstants.BASE_URL已经定义
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
        messageService = retrofit.create(MessageService.class);
    }

    public void sendMessage(Long senderId, Long receiverId, String content, Callback<Message> callback) {
        Call<Message> call = messageService.sendMessage(senderId, receiverId, content);
        call.enqueue(callback);
    }

    public void deleteMessage(Long messageId, Callback<String> callback) {
        Call<String> call = messageService.deleteMessage(messageId);
        call.enqueue(callback);
    }

    public void updateMessageStatus(Long messageId, Boolean isRead, Callback<Message> callback) {
        Call<Message> call = messageService.updateMessageStatus(messageId, isRead);
        call.enqueue(callback);
    }

    public void listMessagesByReceiverId(Long receiverId, Callback<List<Message>> callback) {
        Call<List<Message>> call = messageService.listMessagesByReceiverId(receiverId);
        call.enqueue(callback);
    }

    public void getLatestMessageByFollowerId(Long receiverId, Callback<Message> callback) {
        Call<Message> call = messageService.getLatestMessageByFollowerId(receiverId);
        call.enqueue(callback);
    }

    public void listMessagesBySenderAndReceiverId(Long senderId, Long receiverId, Callback<List<Message>> callback) {
        Call<List<Message>> call = messageService.listMessagesBySenderAndReceiverId(senderId, receiverId);
        call.enqueue(callback);
    }
}
